﻿
#include "CreateConeDlg.h"
#include <vtkConeSource.h>
#include <vtkActor.h>    
#include <vtkPolyDataMapper.h>    



CreateConeDlg::CreateConeDlg(QWidget* parent):
    DlgBase(parent)
{
    initUI();
    manageConnections();
}

CreateConeDlg::~CreateConeDlg()
{
}

bool CreateConeDlg::create()
{
    auto pVtkViewWindow = MyVtkViewWindow::GetInstance();
    if(pVtkViewWindow == nullptr)
    {
        return false;
    }

    {
        m_centerPointCoord->getValues(m_centerPoint[0], m_centerPoint[1], m_centerPoint[2]);
        m_directionEdit->getValues(m_direction[0], m_direction[1], m_direction[2]);
        m_height = m_heightEdit->getValue();
        m_radius = m_radiusEdit->getValue();

        //create vtk vtkLineSource
        vtkSmartPointer<vtkConeSource> coneSource = vtkSmartPointer<vtkConeSource>::New();
        coneSource->SetCenter(m_centerPoint);
        coneSource->SetDirection(m_direction);
        coneSource->SetHeight(m_height);
        coneSource->SetRadius(m_radius);
        coneSource->SetResolution(QtVtkModle::Global::ConeResolution);
        coneSource->Update();

        //create vtk actor
        vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        mapper->SetInputConnection(coneSource->GetOutputPort());
        mapper->StaticOn();

        vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
        actor->GetProperty()->SetColor(QtVtkModle::Global::ConeColor[0], QtVtkModle::Global::ConeColor[1], QtVtkModle::Global::ConeColor[2]);
        actor->SetMapper(mapper);

        //add actor to vtk view
        pVtkViewWindow->GetRenderer()->AddActor(actor);
        GetActorManagerInstance()->AddActor(actor, PickMode::VOLUME);
    }

    pVtkViewWindow->GetRenderer()->ResetCamera();
    pVtkViewWindow->GetRenderWindow()->Render();
    return true;
}

void CreateConeDlg::initUI()
{
    setWindowTitle("Create Cone");
    QVBoxLayout* pDlgLayout = new QVBoxLayout(this);  //create layout

    {
        m_centerPointCoord = new MyCoordinateEdit(this, "Center Point");
        pDlgLayout->addWidget(m_centerPointCoord);

        m_directionEdit = new MyCoordinateEdit(this, "Direction");
        m_directionEdit->setValues(0, 1, 0);
        pDlgLayout->addWidget(m_directionEdit);

        m_heightEdit = new MyDoubleEdit(this, "Height");
        m_heightEdit->setValue(2);
        pDlgLayout->addWidget(m_heightEdit);

        m_radiusEdit = new MyDoubleEdit(this, "Radius");
        m_radiusEdit->setValue(1);
        pDlgLayout->addWidget(m_radiusEdit);
    }

    {
        m_okBtn = new QPushButton("OK", this);
        m_cancelBtn = new QPushButton("Cancel", this);

        QHBoxLayout* okCancelLayout = new QHBoxLayout(this);
        okCancelLayout->addWidget(m_okBtn);
        okCancelLayout->addWidget(m_cancelBtn);
        pDlgLayout->addLayout(okCancelLayout);
    }

    this->setLayout(pDlgLayout);
}

void CreateConeDlg::manageConnections()
{
    connect(m_okBtn, &QPushButton::clicked, this, [=]()
            {
                if(create())
                {
                    close();
                }
                else
                {
                    QMessageBox::warning(this, "Error", "Failed to create cone!");
                }

            });

    connect(m_cancelBtn, &QPushButton::clicked, this, &CreateConeDlg::close);
}



